package com.hiwan.system.common.aspect;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.hiwan.system.common.web.Servlets;
import com.hiwan.system.util.LogUtils;

/**
 * 
 * Datetime   ： 2016年1月6日 下午4:47:44<br>
 * Title      :  LogAspect.java<br>
 * Description:   日志切面，作用在标有@LogRecord的方法上<br>
 * Company    :  hiwan<br>
 * @author cbj
 *
 */
@Aspect
@Component
public class LogAspect {

	private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);

	private static final String LOG_POINTCUT = "@annotation(com.hiwan.framework.dao.annotation.LogRecord)";
	
	@Before(LOG_POINTCUT)
	public void doBefore(JoinPoint joinPoint) {
		try {
			HttpServletRequest request = Servlets.getRequest();
			LogUtils.saveLog(request, "");
		} catch (Exception e) {
			logger.error("异常信息:{}", e.getMessage());
		}
	}

	@AfterThrowing(pointcut = LOG_POINTCUT, throwing = "e")
	public void doAtferThrowException(JoinPoint joinPoint, Throwable e) {
		try {
			HttpServletRequest request = Servlets.getRequest();
			LogUtils.saveLog(request,e,"");
		} catch (Exception ex) {
			logger.error("异常信息:{}", ex.getMessage());
		}
	}

}
